import React from 'react';
import { Form, Input, Button, message } from 'antd';
import { connect } from 'umi';


class PswUpdate extends React.Component<any, any> {
    constructor(props: any) {
        super(props);
        this.state = {
            loading: false,
            datas: {},
            cascaderOption: [],
            contentEditor: ''
        }
    }
    formRef: any = React.createRef();
    componentDidMount() {
        if (this.props.userId) {
            this.getData(this.props.userId);
        }
    }
    getData = (id: number) => {
        const { dispatch } = this.props;
        if (id > 0) {
            dispatch({
                type: 'Abp/get',
                payload: { id },
                control: 'User',
            }).then(() => {
                var result = this.props.response.result;
                this.setState({ datas: result });
                this.formRef.current.setFieldsValue({ user: result });
            });
        }
    }
    onFinish = (values: any) => {
        values.user.userId = this.props.userId;
        this.props.dispatch({
            type: "Abp/changePassword",
            payload: values.user,
        }).then(() => {
            var resp = this.props.response;
            if (resp.success) {
                message.success('密码修改成功！');
            }
            else {
                message.error('保存错误，请联系管理员');
            }
        });
    };
    checkPsw = (rule: any, value: any, callback: any) => {
        if (value) {
            var datas = this.formRef.current.getFieldsValue();
            if (value == datas.user.oldPassword) {
                callback('新密码不能与原始密码相同');
            }
            else if (value != datas.user.confirmPsw && datas.user.confirmPsw) {
                callback('两次密码输入不一致');
            }
            else {
                callback();
            }
        }
        else { callback('请输入新密码'); }
    }
    confirmPsw = (rule: any, value: any, callback: any) => {
        if (value) {
            var datas = this.formRef.current.getFieldsValue();
            if (value != datas.user.newPassword) {
                callback('两次密码输入不一致');
            } else {
                callback();
            }
        }
        callback();
    }
    render() {
        const layout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 17 },
        };
        return (
            <div>
                <Form {...layout} ref={this.formRef} onFinish={this.onFinish} >
                    <Form.Item
                        name={['user', 'oldPassword']}
                        label="原始密码"
                        rules={[{ required: true, message: "请输入原始密码" }]}>
                        <Input.Password />
                    </Form.Item>
                    <Form.Item
                        name={['user', 'newPassword']}
                        label="新密码"
                        rules={[{ required: true, validator: this.checkPsw }]}
                    >
                        <Input.Password />
                    </Form.Item>
                    <Form.Item
                        name={['user', 'confirmPsw']}
                        label="确认密码"
                        rules={[{ required: true, message: "两次密码输入不一致", validator: this.confirmPsw }]}
                    >
                        <Input.Password />
                    </Form.Item>
                    <Form.Item wrapperCol={{ ...layout.wrapperCol, offset: 6 }}>
                        <Button
                            type="primary"
                            htmlType="submit"
                            loading={this.state.loading}
                        >确定</Button>

                    </Form.Item>
                </Form>
            </div>
        )
    }
}
export default connect(({ Abp }: API.IConnectState) => ({
    Abp,
    response: Abp.response,
    currentUser: Abp.currentUser
}))(PswUpdate);